﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DateTimeSettingDescriptorTests.cs" company="Radical Research Ltd">
//   Copyright (c) Radical Research Ltd. The MIT License (MIT)
// </copyright>
// <summary>
//   Unit test for DateTimeSettingDescriptor
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace RadicalResearch.MiniState.Tests
{
    using System;

    using NUnit.Framework;

    using RadicalResearch.MiniState;
    using RadicalResearch.MiniState.Descriptors;

    using Rhino.Mocks;

    /// <summary>Unit test for DateTimeSettingDescriptor</summary>
    [TestFixture]
    internal class DateTimeSettingDescriptorTests
    {
        #region Constants and Fields

        /// <summary>mock repository</summary>
        private MockRepository mocks;

        /// <summary>mock value collection</summary>
        private IValueCollection values;

        #endregion

        #region Public Methods

        /// <summary>Test the constructor</summary>
        [Test]
        public void DateTimeSettingDescriptorConstructor()
        {
            this.mocks.ReplayAll();

            DateTimeSettingDescriptor settingDescriptor = new DateTimeSettingDescriptor("valueKey", true);
            Assert.IsTrue(settingDescriptor.IsValidated);
            Assert.AreEqual("0valueKey", settingDescriptor.ValueKey);
        }

        /// <summary>Test write method</summary>
        [Test]
        public void DateTimeSettingDescriptorWrite()
        {
            Expect.Call(this.values["0valueKey"] = "1");

            this.mocks.ReplayAll();

            DateTimeSettingDescriptor settingDescriptor = new DateTimeSettingDescriptor("valueKey", true);

            DateTime dateTime = new DateTime(2000, 1, 1, 0, 0, 1);

            settingDescriptor.WriteValue(this.values, dateTime);
        }

        /// <summary>Tests writing a value which is before the specified epoch should throw ArgumentOutOfRangeException</summary>
        [Test]
        public void DateTimeSettingDescriptorWriteBeforeEpoc()
        {
            this.mocks.ReplayAll();

            DateTimeSettingDescriptor settingDescriptor = new DateTimeSettingDescriptor("valueKey", true);

            DateTime dateTime = new DateTime(1999, 1, 1, 0, 0, 0);

            Assert.Throws(
                typeof(ArgumentOutOfRangeException), () => settingDescriptor.WriteValue(this.values, dateTime));
        }

        /// <summary>tests writing a value with a specified epoch</summary>
        [Test]
        public void DateTimeSettingDescriptorWriteSpecifyEpoc()
        {
            Expect.Call(this.values["0valueKey"] = "a");

            this.mocks.ReplayAll();

            DateTimeSettingDescriptor settingDescriptor = new DateTimeSettingDescriptor("valueKey", true, 2010);

            DateTime dateTime = new DateTime(2010, 1, 1, 0, 0, 10);

            settingDescriptor.WriteValue(this.values, dateTime);
        }

        /// <summary>test fixture setup</summary>
        [TestFixtureSetUp]
        public void FixtureSetUp()
        {
            this.mocks = new MockRepository();
        }

        /// <summary>test setup</summary>
        [SetUp]
        public void SetUp()
        {
            this.values = this.mocks.StrictMock<IValueCollection>();
        }

        /// <summary>test teardown</summary>
        [TearDown]
        public void TearDown()
        {
            this.mocks.VerifyAll();
        }

        #endregion
    }
}